<template>
	<view class="container">
		<view class="status_bar" :style="{paddingTop:`${menuButtonInfo.top}px`}">
			<view class="title-bar" :style="{height:`${menuButtonInfo.height}px`}">财务报表</view>
		</view>
		<view class="main">
			<view class="div-main">
				<view class="title-class">
					<image src="../../static/image/suer.png" mode="" class="left-oo"></image>
					<text v-if="storeRow.code"
						class="text-span">{{storeRow && storeRow.code }}{{ storeRow && storeRow.name}}</text>
				</view>
				<!-- <picker @change="bindPickerChange" :value="index" :range="array"></picker> -->
				<view class="title-right" @click="changeShowDailog">
					<image src="../../static/image/home_ic_arrange@3x.png" class="left-ppp" mode=""></image>
				</view>

			</view>
			<view class="class-main-content">
				<view class="class-year">
					<text> {{date.split('-')[0]}}年</text>
				</view>
				<view class="coabtb">
					<!-- #ifdef H5 -->
					<van-tabs @change="change" class="self-tabs" v-model="activeName">
						<van-tab class="tabs-chang" v-for="(item,index) of monthArray " :name="index + ''" :key="index"
							:title="item.month +'月'">
						</van-tab>
					</van-tabs>
					<!-- #endif -->
					<!-- #ifdef MP-WEIXIN -->
					<scroll-view class="scroll-view_H tab-scroll" scroll-x="true" scroll-with-animation
						:scroll-left="scrollLeft">
						<view class="scroll-view-item_H uni-bg-red tab-scroll_item" v-for="(item,index) of monthArray "
							:class="[clickIndex == index ? 'active-class' : '', `errorClass-${index}`]"
							@click="clickItem(item, index)" :key="index">
							<text :class="{ 'active-class-text': clickIndex == index }">{{item.month}}月 </text>
							<view class="active-class-selected" v-if="clickIndex == index">
								<image src="/static/image/home_ic_selected@3x.png" class="selected" mode=""></image>
							</view>
						</view>
					</scroll-view>
					<!-- #endif -->
					<picker fields="month" mode="date" :value="date" :start="startDate" :end="endDate"
						@change="bindDateChange">
						<view class="home_ic_down">
							<image src="/static/image/home_ic_down@3x.png" class="selected" mode=""></image>
						</view>
					</picker>
				</view>
				<view class="class-card-uni">
					<view class="uni-left">
						<view class="uni-big">
							{{hanlerDataValue(titeData.yzsk)}}
						</view>
						<view class="uni-down">
							月总收款(元)
						</view>
					</view>
					<view class="uni-right">
						<view class="uni-small">
							<view class="uni-text">
								月毛利(元)
							</view>
							<view class="uni-yuan">
								{{hanlerDataValue(titeData.saleml)}}
							</view>
						</view>
						<view class="uni-small">
							<view class="uni-text">
								月营业额(元)
							</view>
							<view class="uni-yuan">
								{{hanlerDataValue(titeData.saleamount)}}
							</view>
						</view>
					</view>
				</view>
				<!-- zhuti -->
				<view class="white-div">
					<view class="tonbgji">
						收入统计
					</view>
					<view :class="{'card-type':item.show ,'card-type-02':!item.show}" v-for="(item,index) of  tableData"
						:key="index" @click="handlerRow(item)">
						<template v-if="item.show">
							<view class="uni-left-type">
								{{item.type == 1 ? `月收款一(元)` :'月收款二(元)'}}
							</view>
							<view class="uni-right-type">
								<view class="text-uniapp">
									{{item.type == 1 ?  hanlerDataValue(item.allAccount) :hanlerDataValue(item.total)}}
								</view>
								<view class="text-uniapp-icon">
									<text>查看明细</text>
									<image class="kkkkkk" src="/static/image/home_ic_more@3x.png" mode=""></image>
								</view>
							</view>
						</template>

					</view>
					<!-- 	<view class="">
			 			{{ JSON.stringify(loginRes) }}
			 		</view> -->
				</view>

			</view>
			<uniDailog v-if="showDailog" @confim="confim" @close="close" :storeRow="storeRow" :index="index" />
			<waterMark v-if="showMark" />
		</view>

	</view>
</template>

<script>
	import {
		getOpenId,
		getStore,
		getAliUserId,
		getUserPhone,
		getChatUserPhone,
		getReportDataMonthFnbt,
		getReportDataMonth
	} from '../../api';
	import waterMark from '../../components/waterMark.vue'
	import uniDailog from '../../components/dailog.vue'
	import {
		ThousandAndDecimal,
		concurrencyRequest,
		singleSignOn
	} from '../../utils/base.js';
	import {
		monthArray
	} from './config.js'
	export default {
		components: {
			waterMark,
			uniDailog
		},
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				showDailog: false,
				showMark: false,
				show: true,
				monthArray,
				array: [],
				storeData: [],
				params: {
					storeCode: "",
					month: ''
				},
				storeRow: {
					code: '',
					name: ""
				},
				index: 0,
				scrollLeft: 0,
				clickIndex: null,
				monthValue: "",
				date: currentDate,
				HTMLCollection: [],
				activeName: '11',
				titeData: {},
				tableData: [],
				menuButtonInfo: null
			};
		},
		onLoad() {
			// #ifdef MP-WEIXIN
			this.getScrollW()
			this.initLogin()
			this.menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			console.log(this.menuButtonInfo, 'menuButtonInfo')
			// #endif
			// #ifdef H5
			this.$nextTick(() => {
				document.getElementsByClassName('uni-page-head-btn')[0].style.display = "none"
			})
			// #endif
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},

		methods: {
			initLogin() {
				singleSignOn().then(loginRes => {
					this.getOpenId(loginRes.code);
				})
			},
			async getOpenId(loginCode) {
				try {
					const {
						code,
						data,
						errMsg
					} = await getOpenId({
						code: loginCode
					});
					if (+code === 0) {
						// debugger
						const _data = data || {};
						this.handlderUserInfo(_data)
						if (_data.phone) {
							this.handlerFind(this.date)
							this.showMark = true
						} else {
							uni.redirectTo({
								url: '/pages/login/index'
							});
						}

					}
				} catch (error) {
					console.error(error);
				}
			},
			// #ifdef H5
			change(name, title) {
				let month = title.split('月')[0]
				let index = name * 1
				month = (month > 9 ? month : ('0' + month))
				this.date = this.date.split('-')[0] + "-" + month;
				console.log(this.date, "----")
				this.params.month = this.date.replace(/-/, "")
				this.params.storeCode = this.storeRow.code
				this.getDataMonth(this.params)
			},
			// #endif
			close() {
				this.showDailog = false
			},
			async confim(val, {
				index
			}) {
				this.storeRow = val

				this.showDailog = false
				this.index = index
				await this.Refresh(false)
				console.log(val, "---------dasdsadsad", index)
			},
			ThousandAndDecimal,
			hanlerDataValue(val) {
				val = val ? this.ThousandAndDecimal(+val) : '-'
				return val
			},
			// #ifdef MP-WEIXIN
			// 获取标题区域宽度，和每个子元素节点的宽度以及元素距离左边栏的距离
			getScrollW() {
				const query = uni.createSelectorQuery().in(this);
				query.selectAll('.tab-scroll_item').boundingClientRect(data => {
					let dataLen = data.length;
					for (let i = 0; i < dataLen; i++) {
						this.monthArray[i].left = data[i].left;
						this.monthArray[i].width = data[i].width
						// console.log(this.monthArray, '----dasd')
					}
				}).exec()
			},
			// #endif

			//  找到monthArray
			handlerFind(val, value = true) {
				this.monthValue = +(val.split('-')[1])
				let month = this.monthValue
				let index = this.monthArray.findIndex((item, index) => (item.month == month))
				this.clickIndex = index
				this.activeName = this.clickIndex + ''
				this.scrollLeft = 0;
				// #ifdef MP-WEIXIN
				for (let i = 0; i < this.clickIndex - 1; i++) {
					this.scrollLeft += this.monthArray[i].width
				};
				// #endif
				if (value) this.getStore()


				console.log(this.date, month, index, 'this.date')
			},
			bindDateChange: function(e) {
				this.date = e.detail.value
				this.handlerFind(this.date, false)
				this.Refresh(false)

			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				// let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				// day = day > 9 ? day : '0' + day;
				return `${year}-${month}`;
			},
			//   处理门店行数据
			getStoreRow() {
				this.storeRow = this.storeData.find((item, index) => {
					if (index == this.index) {
						return {
							...item
						}
					}
				})
				console.log(this.storeRow, "storeRow")
			},
			//   刷新数据
			Refresh(val = true) {
				if (val) this.getStoreRow();
				this.params.month = this.date.replace(/-/, "")
				this.params.storeCode = this.storeRow.code
				this.getDataMonth(this.params)
			},
			changeShowDailog() {
				this.showDailog = true
			},

			bindPickerChange: function(e) {
				// console.log('picker发送选择改变，携带值为', e.detail.value)
				this.index = e.detail.value
				this.Refresh()
			},
			// 月度报表数据 getReportDataMonth
			async getDataMonth(params) {
				// concurrencyRequest()

				// getReportDataMonthFnbt
				try {
					const {
						data,
						code
					} = await getReportDataMonth(params)
					if (code == "000") {
						// console.log(data, "------")
						this.titeData = data || {}
						this.tableData = [(Object.assign({
								type: 1,
								url: `/pages/collection/index`,
								show: true
							}, data?.ydImpStoreml || {})),
							(Object.assign({
								type: 2,
								url: `/pages/collection/index`,
								show: data?.ydImpStorefnbt && data?.ydImpStorefnbt.total > 0 ? true : false
							}, data?.ydImpStorefnbt || {}))
						]
						// console.log(this.tableData, "this.tableData ")
					}
				} catch (e) {
					//TODO handle the exception
				}
			},
			handlerRow(row) {
				uni.navigateTo({
					url: row.url +
						`?type=${row.type}&month=${this.params.month}&storeCode=${this.params.storeCode}`
				})
				console.log(row)
			},
			clickItem(row, index) {
				let month = '';
				month = (row.month > 9 ? row.month : ('0' + row.month))
				this.date = this.date.split('-')[0] + "-" + month;
				console.log(this.date, 'this.date')
				this.clickIndex = index
				this.activeName = this.clickIndex + ''
				this.scrollLeft = 0;
				// #ifdef MP-WEIXIN
				for (let i = 0; i < this.clickIndex - 1; i++) {
					this.scrollLeft += this.monthArray[i].width
				};
				// #endif
				this.Refresh(false)
			},
			async getStore(params = {
				type: '',
				storeName: ""
			}) {
				// debugger
				try {
					const {
						data,
						code
					} = await getStore(params)
					if (code == "000") {
						this.storeData = data.map(item => {
							if (item.code) {
								item.name2 = item.code + item.name
							}
							return {
								namee: item.name2,
								...item
							}
						})
						this.Refresh()
					}
					// console.log(data)
				} catch (e) {
					//TODO handle the exception
				}

			},



		}
	}
</script>

<style lang="scss" scoped>
	.container {
		.status_bar {
			// height: 180rpx;
			width: 100%;
			display: flex;
			justify-content: center;
			flex-direction: column;
			align-items: center;
			// padding-top: 84rpx;
			padding-right: 44rpx;
			font-size: 28rpx;
			background-color: #fff;

			.title-bar {
				width: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}

		.child {
			width: 80rpx;
			height: 80rpx;
			background: #f2f3f5;
			border-radius: 8rpx;
		}

		width: 100%;
		min-height: calc(100vh );
		// padding: 0 24rpx;
		// border: 2rpx solid;
		background: rgb(250, 250, 250);

		.display-center-space-between {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.display-center-start {
			display: flex;
			align-items: center;
			// justify-content: space-between;
		}

		.class-main-content {
			padding: 24rpx;
			margin-top: 16rpx;
			// height: 152rpx;
			// border: 2rpx solid;

			.class-year {
				position: relative;
				@extend .display-center-start;
				height: 48rpx;
				color: rgb(22, 118, 193);
				font-size: 28rpx;
				font-weight: 400;
				line-height: 48rpx;
				letter-spacing: 0rpx;
				text-align: center;
			}

			.self-tabs {
				::v-deep .van-tab {
					width: 112rpx;
					height: 48rpx;
				}

				::v-deep .van-tab--active {
					border: none;
					background: rgb(22, 118, 193);
					border-radius: 16rpx;
					color: rgb(255, 255, 255);
				}

				::v-deep .van-tabs__line {
					display: none;
				}

				::v-deep .van-tabs__nav {
					background-color: transparent;
				}

				// /deep/
			}

			.scroll-view_H {
				white-space: nowrap;
				width: 100%;
				height: 96rpx;
			}

			.selected {
				width: 32rpx;
				height: 32rpx;
			}

			.active-class-selected {

				position: absolute;
				left: 38rpx;

			}

			.active-class {
				width: 112rpx;
				height: 48rpx;
				position: relative;
				background: rgb(22, 118, 193);
				border-radius: 16rpx;

				.active-class-text {
					color: rgb(255, 255, 255);
					font-size: 28rpx;
					font-weight: 500;
					line-height: 48rpx;
					letter-spacing: 0rpx;
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}

			.uni-bg-red {
				color: rgb(153, 153, 153);
				font-size: 28rpx;
				font-weight: 400;
				line-height: 48rpx;
				letter-spacing: 0rpx;
				text-align: center;
			}

			.white-div {
				margin-top: 24rpx;
				height: 860rpx;
				padding: 24rpx;
				background: rgb(255, 255, 255);
				border-radius: 16rpx;

				.card-type-02 {
					display: none;
				}

				.card-type {
					@extend .display-center-space-between;
					align-items: flex-start;
					padding: 24rpx;
					height: 144rpx;
					margin-top: 24rpx;

					background: rgb(250, 250, 250);

					.uni-left-type {
						height: 48rpx;


						color: rgb(0, 0, 0);

						font-size: 28rpx;
						font-weight: 400;
						line-height: 48rpx;
						letter-spacing: 0rpx;
						text-align: left;


						display: flex;
						flex-direction: row
					}

					.uni-right-type {
						display: flex;
						flex-direction: column;
						align-items: flex-end;

						.text-uniapp {
							height: 48rpx;
							color: rgb(2, 0, 0);
							font-family: OPPO Sans;
							font-size: 28rpx;
							font-weight: 400;
							line-height: 48rpx;
							letter-spacing: 0rpx;
							text-align: left;
						}

						.text-uniapp-icon {
							height: 48rpx;
							color: rgb(153, 153, 153);
							font-size: 24rpx;
							font-weight: 400;
							line-height: 48rpx;
							letter-spacing: 0rpx;
							text-align: left;
							display: flex;
							align-items: center;

							.kkkkkk {
								width: 24rpx;
								height: 24rpx;
							}
						}
					}
				}

				.tonbgji {
					height: 40rpx;
					color: rgb(0, 0, 0);
					font-size: 28rpx;
					font-weight: 400;
					line-height: 40rpx;
					letter-spacing: 0rpx;
					text-align: left;
				}
			}

			.class-card-uni {
				@extend .display-center-space-between;
				height: 184rpx;
				background: rgb(22, 118, 193);
				border-radius: 16rpx;
				padding: 32rpx;

				.uni-right {
					margin-top: 24rpx;

					.uni-small {
						display: flex;
						align-items: center;
						justify-content: flex-end;
					}

					.uni-text {
						color: rgba(255, 255, 255, 0.8);
						font-size: 20rpx;
						font-weight: 400;
						line-height: 48rpx;
						letter-spacing: 0rpx;
						text-align: left;
						margin-right: 28rpx;
					}

					.uni-yuan {
						color: rgb(255, 255, 255);
						font-family: OPPO Sans;
						font-size: 26rpx;
						font-weight: 400;
						line-height: 48rpx;
						letter-spacing: 0rpx;
						text-align: right;
					}


				}

				.uni-left {
					.uni-big {
						height: 48rpx;
						color: rgb(255, 255, 255);
						font-size: 40rpx;
						font-weight: 400;
						line-height: 48rpx;
						letter-spacing: 0rpx;
						text-align: center;
					}

					.uni-down {
						margin-top: 24rpx;
						width: 154rpx;
						height: 48rpx;


						color: rgba(255, 255, 255, 0.8);

						font-size: 24rpx;
						font-weight: 500;
						line-height: 48rpx;
						letter-spacing: 0rpx;
						text-align: center;
					}
				}
			}

			.coabtb {
				white-space: nowrap;
				overflow: scroll;
				margin-top: 8rpx;
				position: relative;
			}

			.home_ic_down {
				position: absolute;
				right: 0rpx;
				// #ifdef MP-WEIXIN
				top: 4rpx;
				background-color: #fff;
				// #endif
				// #ifdef H5
				top: -88rpx;
				background: rgb(250, 250, 250);
				// #endif

				height: 48rpx;

				display: flex;
				align-items: center;
				// border: 2rpx solid;
			}

			.scroll-view-item_H:last-child {
				margin-right: 48rpx;
			}


			.scroll-view-item_H {
				display: inline-block;
				width: 112rpx;
				text-align: center;
				font-size: 28rpx;

			}
		}

		.div-main {
			@extend .display-center-space-between;
			padding: 0 24rpx;
			height: 112rpx;
			background-color: #fff;


			.title-class {
				@extend .display-center-start;

				.left-oo {
					width: 72rpx;
					height: 64rpx;
					filter: grayscale(1);

				}

				.text-span {
					width: 560rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					// white-space: nowrap;

					color: rgb(4, 0, 0);
					font-size: 28rpx;
					font-weight: 500;
					line-height: 48rpx;
					letter-spacing: 0rpx;
					display: flex;
					flex-direction: row;
					align-items: center;
				}
			}

			.title-right {
				.left-ppp {
					width: 48rpx;
					height: 48rpx;
				}
			}
		}
	}
</style>